//
//  WebViewController.m
//  享动
//
//  Created by darker on 16/4/16.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIActivityIndicatorView *activity;
// 没有网络的提示框
@property (nonatomic, strong)  UIView *hint;
@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    self.navigationItem.title = @"2016全球马拉松指南";
    // 点击 2016全球马拉松跳转到的界面
    self.view.backgroundColor = [UIColor whiteColor];
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT + 49)];
    // 判断网络
    if ([[NetWorkState shareInstance]reachability]) {
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://e.thejoyrun.com/mlscalendar/index.html"]]];
    } else {
        [self addHint];
    }
    

    _webView.delegate = self;

    // 为了webView 适应手机屏幕
    [_webView setScalesPageToFit:YES];
    [self.view addSubview:_webView];
    
    _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [_activity setCenter:CGPointMake(KWIDTH / 2, KHEIGHT / 2)];
    [_activity startAnimating];
}

- (void) request
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.geexek.com/enroll/phone/applySquare.html"]];
    [_webView loadRequest:request];
    //    [_activity stopAnimating];
}

// 加载成功
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_activity stopAnimating];
    
}


// 加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error
{
    [self addHint];
}

#pragma mark ---- 没有网络的提示框 ----
- (void)addHint
{
    // 没有网络的提示框
    _hint = [[UIView alloc]initWithFrame:CGRectMake(KWIDTH / 4, KHEIGHT / 2 - KHEIGHT / 15, KWIDTH / 2, KHEIGHT / 15 * 2)];
    _hint.backgroundColor = [UIColor blackColor];
    _hint.alpha = 0.7;
    _hint.layer.cornerRadius = 10;
    [self.view addSubview:_hint];
    
    UIImageView *noNetworkimageView = [[UIImageView alloc]initWithFrame:CGRectMake(_hint.frame.size.width / 3 + 3, 0, _hint.frame.size.width / 3 + 3, _hint.frame.size.width / 3 * 53 / 63)];
    noNetworkimageView.image = [UIImage imageNamed:@"noNetwork"];
    [_hint addSubview:noNetworkimageView];
    
    UILabel *hintLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _hint.frame.size.height / 2, KWIDTH / 2, _hint.frame.size.height / 2)];
    hintLabel.text = @"网络超时";
    hintLabel.textAlignment = NSTextAlignmentCenter;
    hintLabel.textColor = [UIColor whiteColor];
    [_hint addSubview:hintLabel];
    
    [self performSelector:@selector(removeHint) withObject:nil afterDelay:3];
    
}

- (void)removeHint
{
    [_hint removeFromSuperview];
}



- (void)leftBarButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void) viewWillAppear:(BOOL)animated
{
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void) viewWillDisappear:(BOOL)animated
{
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
